/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accesoDatos;

/**
 *
 * @author user
 */

import logica.*;
import java.sql.*;

public class DAOArticuloMemoria {
    
    private FachadaBD fachada;
    
    public DAOArticuloMemoria(){
        fachada=new FachadaBD();
    }
    
    public int guardarArticuloMemoria(ArticuloMemoria art_memo){
        int numFilas=-1;
        /*String sql1="INSERT INTO articulo (nombre, fecha_pub, nom_rev_lib, tipo,id_grupo_inves) VALUES ("
                + art_memo.getNombre() + ", " + art_memo.getFechaPublicacion() + ", "
                + art_memo.getNombreRevista() + ", " + art_memo.getTipo() + ", "
                + art_memo.getIdGrupo() + ")";*/
        
        String sql="INSERT INTO art_memoria (id_articulo,nom_evento,fecha_evento,ciudad) VALUES ('"
                + art_memo.getId() + "', '" + art_memo.getNombreEvento() + "', '" + art_memo.getFechaEvento() + "', '"
                + art_memo.getCiudad() + "')";
        
        System.out.println(sql);
        
        try{
            Connection c=fachada.conectar();
            Statement sentencia=c.createStatement();
            numFilas= sentencia.executeUpdate(sql);
            fachada.desconectar(c);
            System.out.println("articulo de memorio guardado");
            return numFilas;
        }
        catch(SQLException e){System.out.println("SQLExcep: "+e);}
        catch(Exception e){System.out.println("exec" + e);}
        return numFilas;
    }
    
    public ArticuloMemoria consultarArticuloMemo(String id_art_memo){
        ArticuloMemoria art_memo= new ArticuloMemoria();
        
        String consulta_sql1 = "SELECT * FROM articulo WHERE id="+id_art_memo;
        String consulta_sql2 = "SELECT * FROM art_memoria WHERE id="+id_art_memo;
        try{
            Connection c= fachada.conectar();
            Statement sentencia = c.createStatement();
            ResultSet res=sentencia.executeQuery(consulta_sql1);
            
            while(res.next()){
                art_memo.setId(res.getString(1));
                art_memo.setNombre(res.getString(2));
                art_memo.setFechaPublicacion(res.getDate(3));
                art_memo.setNombreRevista(res.getString(4));
                art_memo.setTipo(res.getString(5));
                art_memo.setIdGrupo(res.getString(6));
            }
            
            res=sentencia.executeQuery(consulta_sql2);
            
            while(res.next()){
                art_memo.setNombreEvento(res.getString(2));
                art_memo.setFechaEvento(res.getDate(3));
                art_memo.setCiudad(res.getString(4));
            }
            c.close();
            System.out.println("articulo memoria consulta y Conexion cerrada");
            return art_memo;
        }
        catch(SQLException e){}
        catch(Exception e){}
        
        return null;
    }
}